'''
Created on 2013. 5. 18.

@author: Lee
'''
from google.appengine.ext import ndb
from kr.community.gaesessions import get_current_session
from kr.community.models import Users
import hashlib
import webapp2

class AjaxJoin(webapp2.RequestHandler):
    def get(self):
        return "Internal server error"
    
    def post(self):
        user_email = self.request.POST['user_email']
        user_nick = self.request.POST['user_nickname']
        user_passwd = self.request.POST['passwd']
        user_passwd_cf = self.request.POST['passwd_confirm']
        result_msg = "Success"

        import re
        if (not user_email) or (not user_nick) or (not user_passwd) or (not user_passwd_cf):
            result_msg = "Form is must fill with information."
        elif not re.match(r"[^@]+@[^@]+\.[^@]+", user_email):
            result_msg = "Wrong email format"
        elif user_passwd != user_passwd_cf:
            result_msg = "Wrong password"
        else:
            user_query = Users.query(ndb.OR(Users.user_email == user_email,
                                            Users.user_nickname == user_nick))
            if not user_query.count() == 0:
                result_msg = "Nickname or Email is registered already."

        self.response.write(result_msg)
        
class AjaxLogin(webapp2.RequestHandler):
    def get(self):
        return "Internal server error"
    
    def post(self):
        user_email = self.request.POST['user_email']
        user_passwd = self.request.POST['passwd']
        result_msg = "Success"

        import re
        if (not user_email) or (not user_passwd):
            result_msg = "Form is must fill with information."
        elif not re.match(r"[^@]+@[^@]+\.[^@]+", user_email):
            result_msg = "Wrong email format"
        else:
            user_query = Users.query(Users.user_email == user_email)
            if user_query.count() == 0:
                result_msg = "Not existing email"
            else:
                user_entry = user_query.get()
                hash_func = hashlib.md5(user_passwd)
                if user_entry.user_passwd != hash_func.hexdigest():
                    result_msg = "Wrong password"
                elif user_entry.user_auth == 0:
                    result_msg = "Your account is NOT certified."

        self.response.write(result_msg)
        
class AjaxChangePwd(webapp2.RequestHandler):
    def get(self):
        return "Internal server error"
    
    def post(self):
        user_passwd = self.request.POST['user_passwd']
        new_passwd = self.request.POST['new_passwd']
        new_passwd_cfm = self.request.POST['new_passwd_cfm']
        result_msg = "Success"

        if (not user_passwd) or (not new_passwd) or (not new_passwd_cfm):
            result_msg = "Form is must fill with information."
        else:
            session = get_current_session()
            user_key = ndb.Key(urlsafe=session['user'])
            user_entry = user_key.get() 
            hash_func = hashlib.md5(user_passwd)
            
            if user_entry.user_passwd != hash_func.hexdigest():
                result_msg = "Wrong password"
            elif user_entry.user_auth == 0:
                result_msg = "Your account is NOT certified."
            elif new_passwd != new_passwd_cfm:
                result_msg = "New password is different"
                
        self.response.write(result_msg)